<?php
class SleekCMS_AdminEditContentModule extends Module {
	public function run ($params) {
		$cID	= isset($params['contents_id']) ? $params['contents_id'] : false;
		$ctID	= isset($params['content_types_id']) ? $params['content_types_id'] : false;

		# 404 if no ID
		if (!$ctID or !$cID) {
			return FOF_PAGE;
		}

		# Get the content type
		$contentType = ContentTypesModel::getByID($ctID);

		# 404 if it doesn't exist
		if (!$contentType) {
			return FOF_PAGE;
		}

		# Get the content
		$content = ContentsModel::getByID($cID);

		# 404 if it doesn't exist or belongs to different content type
		if (!$content or ($content->getContentTypesId() != $ctID)) {
			return FOF_PAGE;
		}

		# Update contents
		if (isset($_POST['edit_content'])) {
			ContentsModel::update($_POST['contents_id'], $_POST); # TODO: should use ContentModel instead
			redirect('?updated_content');
		}

		# Assign template vars
		$this->setData('content_type', $contentType);

		# Now get the content
		$this->setData('content', $content);
	}
}
